package erz.printing.police.notch;

import android.graphics.Rect;
import android.os.Build;
import android.view.Window;

import java.util.List;

public class NotchCompat {
    private static INotchScreenSupport mNotchScreenSupport = null;

    /**
     * 判断屏幕是否为凹口屏
     * WindowInsets在View Attach到Window上之后才会创建
     * 因此想要获得正确的结果，方法的调用时机应在DecorView Attach之后
     */
    public static boolean hasDisplayCutout(Window window) {
        checkScreenSupportInit();
        return mNotchScreenSupport.hasNotchInScreen(window);
    }

    /**
     * 获取凹口屏大小
     */
    public static List<Rect> getDisplayCutoutSize(Window window) {
        checkScreenSupportInit();
        return mNotchScreenSupport.getNotchSize(window);
    }

    /**
     * 设置始终使用凹口屏区域
     */
    public static void immersiveDisplayCutout(Window window) {
        checkScreenSupportInit();
        mNotchScreenSupport.setWindowLayoutAroundNotch(window);
    }

    /**
     * 设置始终不使用凹口屏区域
     */
    public static void blockDisplayCutout(Window window) {
        checkScreenSupportInit();
        mNotchScreenSupport.setWindowLayoutBlockNotch(window);
    }

    private static void checkScreenSupportInit() {
        if (mNotchScreenSupport != null) return;

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            mNotchScreenSupport = new DefaultNotchScreenSupport();
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
            mNotchScreenSupport = new PNotchScreenSupport();
        else {
            if (RomUtils.isMiuiRom())
                mNotchScreenSupport = new MiNotchScreenSupport();
            else if (RomUtils.isHuaweiRom())
                mNotchScreenSupport = new HwNotchScreenSupport();
            else if (RomUtils.isOppoRom())
                mNotchScreenSupport = new OppoNotchScreenSupport();
            else if (RomUtils.isVivoRom())
                mNotchScreenSupport = new VivoNotchScreenSupport();
            else
                mNotchScreenSupport = new DefaultNotchScreenSupport();
        }
    }
}
